﻿// -----------------------------------------------------------------------
// <copyright file="BlackBoxManagerTest.cs" company="YUMMO">
// Copyright (c) 2013 Lambert van Lieshout, YUMMO Software Development
// </copyright>
// -----------------------------------------------------------------------

namespace Yummo.BlackBox.Test
{
    using System;
    using Xunit;

    public class BlackBoxManagerTest
    {
        [Fact]
        public void CreateWriterTroughDelegate()
        {
            Assert.DoesNotThrow(delegate
            {
                // Set up the log manager.
                BlackBoxManager log = new BlackBoxManager();
                
                // Create a simple writer with console output.
                log.RegisterWriter(BlackBoxUtilities.GetAllEventTypes(), (t =>
                {
                    System.Console.WriteLine(t.Data);
                }));

                // Write event.
                log.Write(EventTypes.Trace, "Testing");
            });
        }

        [Fact]
        public void WriteCountTest()
        {
            // Set up the log manager.
            BlackBoxManager log = new BlackBoxManager();

            // Set up a mockup event writer.
            EventMockWriter writer = new EventMockWriter();

            // Register mockup writer.
            log.RegisterWriter(BlackBoxUtilities.GetAllEventTypes(), writer.Write);

            // Write event.
            log.Write(EventTypes.Trace, "Testing");

            // Check if the event is written.
            Assert.Equal<int>(1, writer.EventMessages.Count);
        }

        [Fact]
        public void WriteOneButNotAnotherCountTest()
        {
            // Set up the log manager.
            BlackBoxManager log = new BlackBoxManager();

            // Set up a mockup event writer.
            EventMockWriter writer = new EventMockWriter();

            // Register mockup writer.
            log.RegisterWriter(EventTypes.Critical, writer.Write);

            // Write event.
            log.Write(EventTypes.Trace, "Trace testing");
            log.Write(EventTypes.Critical, "Critical testing");

            // Check if the event is written.
            Assert.Equal<int>(1, writer.EventMessages.Count);
        }

        [Fact]
        public void FallbackWriteTest()
        {
            // Set up the log manager.
            BlackBoxManager log = new BlackBoxManager();

            // Set up a mockup event writer.
            EventMockWriter fallbackWriter = new EventMockWriter();

            // Register mockup writer for fallback.
            log.RegisterFallbackWriter(fallbackWriter.Write);

            // Create a exception throwing writer for testing purposes.
            log.RegisterWriter(BlackBoxUtilities.GetAllEventTypes(), (t =>
            {
                throw new Exception("Failed writing!");
            }));

            // Write event.
            log.Write(EventTypes.Trace, "Trace testing");

            // Check if the event is written and the error of the first writer (total 2 events expected).
            Assert.Equal<int>(2, fallbackWriter.EventMessages.Count);
        }

        [Fact]
        public void WriteToMultipleWritersTest()
        {
            // Set up the log manager.
            BlackBoxManager log = new BlackBoxManager();

            // Set up a mockup event writer.
            EventMockWriter fallbackWriter = new EventMockWriter();
            EventMockWriter writerA = new EventMockWriter();
            EventMockWriter writerB = new EventMockWriter();

            // Register mockup writer for fallback.
            log.RegisterFallbackWriter(fallbackWriter.Write);
            log.RegisterWriter(BlackBoxUtilities.GetAllEventTypes(), writerA.Write);
            log.RegisterWriter(BlackBoxUtilities.GetAllEventTypes(), writerB.Write);

            // Write event.
            log.Write(EventTypes.Trace, "Trace testing");

            // Check if the event is written and the error of the first writer (total 2 events expected).
            Assert.Equal<int>(0, fallbackWriter.EventMessages.Count);
            Assert.Equal<int>(1, writerA.EventMessages.Count);
            Assert.Equal<int>(1, writerB.EventMessages.Count);
        }
    }
}
